package u1project.client;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainClient {
    //所有会变的东西尽量挪到外面
    public static void main(String[] args)  {
        Socket s =null;
        Properties p=new Properties();
        try {
            p.load(MainClient.class.getClassLoader().getResourceAsStream("u1project/client.properties"));
            //jar
            //>>p.load(new FileInputStream("javaadv/src/u1project/client.properties"));
            System.out.println(p);
            //s=new Socket("localhost",8888);
            s=new Socket(p.getProperty("ip"), Integer.valueOf(p.getProperty("port")));
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectInputStream oin = new ObjectInputStream(in);
            ObjectOutputStream oout = new ObjectOutputStream(out);
            Scanner input = new Scanner(System.in);
            int choice = 0;
            while (true) {
                System.out.println("1:login 2:注册 9:退出");
                choice = input.nextInt();
                if (choice == 1) {
                    Request r = new Request();
                    r.setAction("login");
                    System.out.println("input name:");
                    r.put("name", input.next());
                    System.out.println("input pass:");
                    r.put("pass", input.next());
                    oout.writeObject(r);
                    oout.flush();
                    oout.reset();
                    Response resp= (Response) oin.readObject();
                    if(resp.isSuccess()){
                        System.out.println("登录成功");
                    }else{
                        System.out.println("登录失败，原因是："+resp.getMsg());
                    }
                }else if(choice==2){
                    Request r = new Request();
                    r.setAction("register");
                    System.out.println("input name:");
                    r.put("name", input.next());
                    System.out.println("input pass:");
                    r.put("pass", input.next());
                    System.out.println("repeat pass:");
                    r.put("repeatpass", input.next());
                    oout.writeObject(r);
                    oout.flush();
                    oout.reset();
                    Response resp= (Response) oin.readObject();
                    if(resp.isSuccess()){
                        System.out.println("登录成功");
                    }else{
                        System.out.println("登录失败，原因是："+resp.getMsg());
                    }
                } else if(choice==9){
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
}
